<?php

class aftersales_data_return_policy extends b2c_api_rpc_request {

    public $rProduct;

    private $arr_status = array(
        '1' => '申请中',
        '2' => '审核中',
        '3' => '审核通过',
        '4' => '完成',
        '5' => '审核未通过'
    );

    public function __construct($app) {
        $this->app = $app;
        $this->rProduct = $this->app->model('return_product');
    }

    public function get_conf_data(&$arr_settings) {
        $arr_settings['return_product_comment'] = $this->app->getConf('site.return_product_comment');
        
        return ($arr_settings && is_array($arr_settings)) ? true : false;
    }

    public function get_return_product_list($clos = '*', $filter = array(), $nPage = 1) {
        $arr_return_products = array();
        
        $aData = $this->rProduct->getList($clos, $filter, ($nPage - 1) * 10, 10, 'add_time DESC');
        $count = $this->rProduct->count($filter);
        
        return $arr_return_products = array(
            'data' => $aData,
            'total' => $count
        );
    }

    public function change_status(&$sdf) {
        $is_changed = $this->rProduct->change_status($sdf);
        
        if ($is_changed) {
            $arr_data = $this->rProduct->dump($sdf['return_id']);
            $sdf['return_id'] = $arr_data['return_id'];
            $sdf['order_id'] = $arr_data['order_id'];
            $sdf['status'] = $arr_data['status'];
            
            return $sdf['status'];
        } else {
            return false;
        }
    }

    public function save_return_product(&$sdf, &$msg = '') {
        $sdf['return_id'] = $this->rProduct->gen_id();
        $sdf['return_bn'] = $sdf['return_id'];
        $is_save = $this->rProduct->save($sdf);
        
        if (! $is_save) {
            $msg = '数据保存失败！';
            
            return false;
        }
        
        return true;
    }

    public function get_return_product_by_return_id($return_id = 0) {
        if (! $return_id)
            return array();
        
        $arr_data = $this->rProduct->dump($return_id);
        
        if ($arr_data) {
            $arr_data['product_data'] = unserialize($arr_data['product_data']);
            $arr_data['comment'] = unserialize($arr_data['comment']);
            $arr_data['status'] = $this->arr_status[$arr_data['status']];
        }
        return $arr_data;
    }

    public function file_download($return_id = 0) {
        if ($return_id) {
            $rp = $this->app->model('return_product');
            
            $is_remote = false;
            $info = $rp->dump($return_id);
            $filename = $info['image_file'];
            $obj_images = app::get('image')->model('image');
            $arr_image = $obj_images->dump($filename);
            if (strpos($arr_image['url'], 'http://') === false)
                $filename = ROOT_DIR . '/' . $arr_image['url'];
            else {
                $is_remote = true;
                $filename = $arr_image['url'];
                $basename = substr($arr_image['url'], strrpos($arr_image['url'], '/') + 1);
            }
            
            if ($filename) {
                if (! $is_remote) {
                    $file = fopen($filename, "r");
                    Header("Content-type: image/jpeg");
                    Header("Accept-Ranges: bytes");
                    Header("Accept-Length: " . filesize($filename));
                    Header("Content-Disposition: attachment; filename=" . basename($filename));
                    echo fread($file, filesize($filename));
                    fclose($file);
                } else {
                    Header("Content-type: image/jpeg");
                    Header("Accept-Ranges: bytes");
                    // Header("Accept-Length: ".filesize($filename));
                    Header("Content-Disposition: attachment; filename=" . $basename);
                    $obj_base_http = kernel::single('base_httpclient');
                    echo $obj_base_http->action('GET', $filename);
                }
            }
        }
    }

    public function order_products_quantity($order_id) {
        $products = app::get('b2c')->model('products');
        $order_delivery = app::get('b2c')->model('order_delivery');
        $aftersales_products = app::get('aftersales')->model('return_product');
        
        $result = $order_delivery->getList('*', array(
            'order_id' => $order_id,
            'dlytype' => 'delivery'
        ));
        $order_delivery_send_product = array();
        foreach ($result as $val) {
            $product_goods = unserialize($val['items']); // 有些订单可能拿不到对应的数据
                                                         
            // 如果存在 $product_goods['product_id'] ，说明是老的错误数据
            if (empty($product_goods) or isset($product_goods['product_id'])) {
                $b2c_delivery = app::get('b2c')->model('delivery');
                $subsdf = array(
                    'delivery_items' => array(
                        '*'
                    )
                );
                $sdf_delivery = $b2c_delivery->dump($val['dly_id'], '*', $subsdf);
                if ($sdf_delivery) {
                    foreach ($sdf_delivery['delivery_items'] as $product) {
                        $order_delivery_product_id = $product['product_id'];
                        if (empty($order_delivery_send_product[$order_delivery_product_id])) {
                            $order_delivery_send_product[$order_delivery_product_id] = $product['number'];
                        } else {
                            $order_delivery_send_product[$order_delivery_product_id] += $product['number'];
                        }
                    }
                }
            } else {
                // 20180528 这里有bug，会导致售后无法显示商品
                if (isset($product_goods['product_id'])) {
                    $product_goods['products'] = $product_goods;
                    $new_product_goods = array(
                        $product_goods
                    );
                    $product_goods = $new_product_goods;
                }
                
                foreach ($product_goods as $product) {
                    $product['send'] = $product['send'] ? $product['send'] : $product['number'];
                    $order_delivery_product_id = $product['product_id'] ? $product['product_id'] : $product['products']['product_id'];
                    if (empty($order_delivery_send_product[$order_delivery_product_id])) {
                        $order_delivery_send_product[$order_delivery_product_id] = $product['send'];
                    } else {
                        $order_delivery_send_product[$order_delivery_product_id] += $product['send'];
                    }
                }
            }
        }
        
        $result = $aftersales_products->getList('*', array(
            'order_id' => $order_id
        ));
        foreach ($result as $val) {
            
            // 忽略被拒绝的售后单
            if ($val['status'] == '5')
                continue;
            
            $product_goods = unserialize($val['product_data']);
            foreach ($product_goods as $val) {
                $product_id = '';
                if (! empty($val['product_id']))
                    $product_id = $val['product_id'];
                if (empty($product_id)) {
                    $result_product = $products->getRow('product_id', array(
                        'bn' => $val['bn']
                    ));
                    $product_id = $result_product['product_id'];
                }
                if (empty($product_id))
                    continue;
                $order_delivery_send_product[$product_id] -= $val['num'];
            }
        }
        foreach ($order_delivery_send_product as $key => $val) {
            if ($val <= 0)
                unset($order_delivery_send_product[$key]);
        }
        return $order_delivery_send_product;
    }

    public function is_order_aftersales($order_id, $member_id = '0') {
        if ($member_id) {
            $check_result = $this->check_is_order_aftersales($order_id, $member_id);
            if (! $check_result) {
                return false;
            }
        }
        $result = $this->order_products_quantity($order_id);
        if (is_array($result) && count($result)) {
            return true;
        } else {
            return false;
        }
    }

    // 下单后60天内可以售后
    public function check_is_order_aftersales($order_id, $member_id) {
        $order = app::get('b2c')->model('orders');
        $order_status['pay_status'] = array(
            1,
            4
        );
        $order_status['createtime|bthan'] = time() - 60 * 24 * 3600;
        $order_status['ship_status'] = array(
            1,
            2,
            3
        );
        $order_status['order_id'] = $order_id;
        $order_status['member_id'] = $member_id;
        $order_status['status'] = array(
            'active',
            'finish'
        );
        
        $aData = $order->getRow('order_id', $order_status);
        if (! $aData) {
            return false;
        } else {
            return true;
        }
    }
}
